<?php
namespace app\common\logic;

use think\facade\Env;
use alioss\OssAli;
use Chumper\Zipper\Zipper;
use IosPngParser\Parser;

class General extends Base {
    
    /**
     * @desc 上传到阿里云
     * @param string $key
     * @param string $type 文件类型
     * @return array|bool
     * @throws \OSS\Core\OssException
     */
    public function upload($key = 'file', $type = 'images') {
        $file = request()->file($key);
        if(!$file) {
            $this->errorCode = EC_UPLOAD_ILLEGAL;
            return false;
        }
        
        $siteName = Env::get('app.site_name');
        $runTimePath = Env::get('runtime_path');
        $saveName   = uniqid(date('YmdHis'));
    
        switch($type) {
            case 'app': {
                $fullPath   = $runTimePath . 'upload/app/' . date('Ym/');
                $objectPath = $siteName . '/app/' . date('Ym/');
                $info = $file->validate([
                    'size' => 1024 * 1024 * 10,
                    'ext'  => 'apk,ipa',
                ])->move($fullPath, $saveName);
                break;
            }
            case 'p8': {
                $fullPath   = $runTimePath . 'upload/p8/' . date('Ym/');
                $objectPath = $siteName . '/p8/' . date('Ym/');
                $info = $file->validate([
                    'size' => 1024 * 1024 * 1,
                    'ext'  => 'p8',
                ])->move($fullPath, $saveName);
                break;
            }
            default: {
                $fullPath   = $runTimePath . 'upload/images/' . date('Ym/');
                $objectPath = $siteName . '/images/' . date('Ym/');
                $info = $file->validate([
                    'size' => 1024 * 1024 * 2,
                    'ext'  => 'jpg,png,gif,jpeg,ico',
                ])->move($fullPath, $saveName);
                break;
            }
        }
        
        if($info) {
            $fullName  = $fullPath. $info->getSaveName();
            $objectName = $objectPath. $info->getSaveName();
            
            //上传到阿里云OSS存储对象
            $OSS = new OssAli();
            $url = $OSS->uploadFile($objectName, $fullName);
            
            return [
                'url' => $url
            ];
        } else {
            $this->errorCode = EC_UPLOAD_ERROR;
            return false;
        }
    }
    
    
    /**
     * @desc 分片上传到阿里云
     * @param string $key
     * @param string $type 文件类型
     * @return array|bool
     * @throws \OSS\Core\OssException
     */
    public function uploadOss($key = 'file', $type = 'other') {
        $file = request()->file($key);
        if(!$file) {
            $this->errorCode = EC_UPLOAD_ILLEGAL;
            return false;
        }
        
        $siteName = Env::get('app.site_name');
        $runTimePath = Env::get('runtime_path');
        
        if($type == 'app') {
            $fullPath   = $runTimePath . 'upload/app/' . date('Ym/');
            $objectPath = $siteName . '/app/' . date('Ym/');
            $saveName   = uniqid(date('YmdHis'));
            $info = $file->validate([
                'size' => 1024 * 1024 * 10,
                'ext'  => 'apk,ipa',
            ])->move($fullPath, $saveName);
            
        }else {
            $fullPath =  $runTimePath. 'upload/images/' .date('Ym/');
            $objectPath = $siteName.'/images/'.date('Ym/');
            $saveName = uniqid(date('YmdHis'));
            
            $info = $file->validate([
                'size' => 1024 * 1024 * 200,
                'ext'  => 'ipa',
            ])->move($fullPath, $saveName);
        }
        if($info) {
            $fullName  = $fullPath. $info->getSaveName();
            $objectName = $objectPath. $info->getSaveName();
            
            //上传到阿里云OSS存储对象
            $OSS = new OssAli();
            // $url = $OSS->uploadFile($objectName, $fullName);
            $url = $OSS->uploadOss($objectName, $fullName);
            
            return [
                'url' => $url
            ];
        } else {
            $this->errorCode = EC_UPLOAD_ERROR;
            return false;
        }
    }
    
    
    /**
     * @desc 上传文件到本地
     * @param string $key
     * @param string $type 文件类型
     * @return array|bool
     */
    public function uploadLocal($key = 'file', $type = 'other') {
        $file = request()->file($key);
        if(!$file) {
            $this->errorCode = EC_UPLOAD_ILLEGAL;
            return false;
        }
        
        $runTimePath = Env::get('runtime_path');
        
        if($type == 'ipa' || $type == 'apk') {
            $path =  $runTimePath. 'upload/'.$type.'/' .date('Ym/');
            $saveName = uniqid(date('YmdHis'));
            $info = $file->validate([
                'size' => 1024 * 1024 * 200, //ipa和apk限制200M
                'ext'  => 'apk,ipa',
            ])->move($path, $saveName);
            
        }else {
            $path =  $runTimePath. 'upload/images/' .date('Ym/');
            $saveName = uniqid(date('YmdHis'));
            $info = $file->validate([
                'size' => 1024 * 1024 * 2,
                'ext'  => 'jpg,png,gif,jpeg,pfx,ico',
            ])->move($path, $saveName);
        }
        
        if($info) {
            $fullName  = $path. $info->getSaveName();
            return [
                'fullName' => $fullName
            ];
        } else {
            $this->errorCode = EC_UPLOAD_ERROR;
            return false;
        }
    }
    
    /**
     * @desc 上传excel文件
     * @param string $key
     * @return array|bool
     */
    public function uploadExcel($key = 'file') {
        $file = request()->file($key);
        if(!$file) {
            $this->errorCode = EC_UPLOAD_ILLEGAL;
            return false;
        }
        
        $runTimePath = Env::get('runtime_path');
        $path = 'upload'.DIRECTORY_SEPARATOR.'excel'.DIRECTORY_SEPARATOR.date('Ym').DIRECTORY_SEPARATOR;
        $saveName = uniqid(date('YmdHis'));
        
        $info = $file->validate([
            'size' => 1024 * 1024 * 2, //最大限制2M
            'ext'  => 'xlsx',
        ])->move($runTimePath. $path, $saveName);
        
        if($info) {
            $fullName  = $runTimePath. $path. $info->getSaveName();
            return [
                'fullName' => $fullName,
                'saveName' => $info->getSaveName(),
                'info'     => $info->getInfo(),
                'md5'      => md5_file($fullName)
            ];
        } else {
            $this->errorCode = EC_UPLOAD_ILLEGAL;
            return false;
        }
    }
}